package com.timesheet.gui.efforttracker.controllers;

import java.util.Date;
import java.util.List;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.DateTime;

import com.example.timesheet.uitl.DateUtil;
import com.timesheet.gui.efforttracker.composite.WSRDateChooseComposite;
import com.timesheet.gui.efforttracker.container.UtilisationModelContainer;
import com.timesheet.server.common.models.EffortTrackerDTO;
import com.timesheet.server.common.session.Session;
import com.timesheet.serverice.UtlisationService;

public class WSRDateChooseController {

	UtilisationModelContainer utilisationModelContainer = UtilisationModelContainer
			.getInstance();
	private WSRDateChooseComposite wsrDateChooseComposite;

	public WSRDateChooseController(WSRDateChooseComposite wsrDateChooseComposite) {
		this.wsrDateChooseComposite = wsrDateChooseComposite;
		performActions(wsrDateChooseComposite);
	}

	void performActions(WSRDateChooseComposite wsrDateChooseComposite) {
		wsrDateChooseComposite.getBtnSubmit().addSelectionListener(
				new SelectionAdapter() {

					@Override
					public void widgetSelected(SelectionEvent e) {
						save();
					}

				});
	}

	private void save() {
		System.out.println("saved");
		DateTime dtWsrDate = wsrDateChooseComposite.getDtWsrDate();
		DateTime dateTo = wsrDateChooseComposite.getDateTo();

		int fromDay = dtWsrDate.getDay();
		int fromMonth = dtWsrDate.getMonth();
		int fromYear = dtWsrDate.getYear();

		Date FromDate = DateUtil.createDate3(fromDay, fromMonth, fromYear)
				.getTime();

		int day = dateTo.getDay();
		int toMonth = dateTo.getMonth();
		int toYear = dateTo.getYear();

		Date toDate = DateUtil.createDate3(day, toMonth, toYear).getTime();
		utilisationModelContainer.setFromDate(FromDate);
		utilisationModelContainer.setToDate(toDate);

		List<EffortTrackerDTO> tmp = new UtlisationService()
				.getUtilisationBasedOnTheProjects(FromDate, toDate,
						Session.INSTANCE.getProjects());
		System.out.println("utilisationList:: " + tmp);
		utilisationModelContainer.setUtilisationList(tmp);
		utilisationModelContainer.refresh();

	}

}
